#include <commands\server\s_reject_connection.h>
#include <app\states\connectionState.h>

#include <iostream>

using namespace std;

REGISTER_COMMAND(SERVER_REJECT_CONNECTION, s_reject_connection)

s_reject_connection::s_reject_connection() : command(0, NULL) {
}

s_reject_connection::s_reject_connection(unsigned int _type_id, streamingConnection *_connection) : command(_type_id, _connection) {
}

void 
s_reject_connection::decode(stringstream &bytes_stream, int size) {
#ifdef CLIENT
  
  #ifdef DEBUG_INFO
  std::cerr << "CLIENT DECODE: s_reject_connection" << std::endl;
  #endif

  streamingConnection * conn = getconnection();
  connectionState * cstate = GET_STATE(connectionState, conn);

  if (cstate) cstate->setLoginAccepted(false);

  #ifdef DEBUG_INFO
  cerr << "Connection to streaming server rejected." << endl;
  #endif

#endif
}

void 
s_reject_connection::execute() {
#ifdef SERVER

  #ifdef DEBUG_INFO
  std::cerr << "SERVER EXECUTE: s_reject_connection" << std::endl;
  #endif

  unsigned int *id = new unsigned int[1];
  unsigned int *total_size = new unsigned int[1];

  total_size[0] = sizeof(unsigned int); // total size itself

  id[0] = getTypeId();
  total_size[0] += sizeof(unsigned int); // id size

  sendBytes(total_size, sizeof(unsigned int));
  sendBytes(id, sizeof(unsigned int));

#endif
}

